package com.semlab.client.fwk.ap;

import java.util.ArrayList;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class ContainerMapping {

	private String id;
	private ArrayList<MappingsEntry> placeMappings = new ArrayList<MappingsEntry>();
	private AcceptsOneWidget widget;
	private ActivityManager activityManager;
	
	public ContainerMapping(String id, AcceptsOneWidget widget) {
		this.id = id;
		this.widget = widget;
	}

	public ContainerMapping(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}
	
	public ArrayList<MappingsEntry> getPlaceMappings() {
		return placeMappings;
	}

	public void addMappingEntry(MappingsEntry entry){
		for (MappingsEntry mapping : placeMappings) {
			if(mapping.getPlace().equals(entry.getPlace())){
				if(mapping.getActivityMapper() == null){
					mapping.setActivityMapper(entry.getActivityMapper());
				}
				return;
			}
		}
		placeMappings.add(entry);
	}

	public AcceptsOneWidget getContainerWidget() {
		return widget;
	}
	
	public void setWidget(AcceptsOneWidget widget) {
		this.widget = widget;
	}

	public void setActivityManager(ActivityManager masterActivityManager) {
		this.activityManager = masterActivityManager;
	}

	public void removeManager() {
		if(activityManager != null){
			activityManager.setDisplay(null);
		}
	}
	
	public MappingsEntry getMappingsFor(Place place){
		for (MappingsEntry mappings : placeMappings) {
			if(mappings.getPlace().equals(place)){
				return mappings;
			}
		}
		return null;
	}
	
	@Override
	public String toString() {
		return "ContainerMapping [id=" + id + ", placeMappings=" + placeMappings + ", widget=" + widget + "]";
	}
}
